<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px">
      <el-row :gutter="24" style="margin-bottom: -15px">
        <el-col :span="24" :xs="24">
          <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="100px"
          >
            <el-form-item label="项目名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入"
                clearable
                style="width: 180px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="项目所属公司" prop="belongingCompanyId">
              <el-select
                v-model="queryParams.belongingCompanyId"
                placeholder="请选择"
                clearable
                @change="handleQuery"
              >
                <el-option
                  v-for="item in companiesList"
                  :key="item.belongingCompanyId"
                  :label="item.name"
                  :value="item.belongingCompanyId"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目时长">
              <el-date-picker
                clearable
                v-model="value1"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                :editable="false"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                @change="handleQuery"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="项目是否结束" prop="state">
              <el-select
                v-model="queryParams.state"
                placeholder="请选择"
                style="width: 100px"
                clearable
                @change="handleQuery"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="float: right">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <stylizedTable
        title="项目管理"
        :buttonList="buttonList"
        :statusColor="true"
        :selector="false"
        :ordinal="true"
        :queryParams="queryParams"
        :loading="loading"
        :total="total"
        :tableItem="tableItem"
        :tableData="ContractProjectList"
        @reset="getList"
        @getList="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {
  listContractProject,
  getContractProject,
  delContractProject,
  addContractProject,
  updateContractProject,
} from "@/api/contract/ContractProject";
import { listBelongingCompany } from "@/api/contract/BelongingCompany";
export default {
  name: "ContractProject",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      ContractProjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        belongingCompanyId: null,
        startTime: null,
        endTime: null,
        state: null,
        addUserId: null,
        addTime: null,
        updateUserId: null,
      },
      buttonList: [
        {
          icon: "el-icon-circle-plus-outline",
          type: "primary",
          text: "新增项目",
          methods: this.handleAdd,
        },
      ],
      tableItem: [
        {
          porp: "项目名称",
          data: "name",
          fixed: "left",
          width: 120,
        },
        {
          porp: "项目所属公司",
          data: "belongingCompanyIdName",
          width: 170,
        },
        {
          porp: "项目开始时间",
          data: "startTime",
          width: 120,
        },
        {
          porp: "项目结束时间",
          data: "endTime",
          width: 120,
        },
        {
          porp: "项目是否结束",
          data: "state",
          width: 120,
          solt: {
            name: 3,
            state: [
              {
                type: "success",
                text: "进行中",
              },
              {
                type: "info",
                text: "已结束",
              },
            ],
          },
        },
        {
          porp: "创建者",
          data: "addUserIdName",
          width: 120,
        },
        {
          porp: "创建时间",
          data: "addTime",
          width: 170,
        },
        {
          porp: "修改者",
          data: "updateUserIdName",
          width: 170,
        },
        {
          porp: "修改时间",
          data: "updateTime",
          width: 170,
        },
        {
          porp: "备注",
          data: "remark",
          width: 170,
          solt: {
            name: 1,
          },
        },
        {
          porp: "操作",
          fixed: "right",
          width: 120,
          solt: {
            name: 5,
            operate: [
              {
                text: "查看",
                icon: "see",
                methods: this.getRouter,
              },
              {
                text: "修改",
                icon: "editTable",
                methods: this.putRouter,
              },
              {
                text: "删除",
                icon: "del",
                methods: this.handleDelete,
              },
            ],
          },
        },
      ],
      value1: "",
      options: [
        {
          label: "进行中",
          value: "进行中",
        },
        {
          label: "已结束",
          value: "已结束",
        },
      ],
      //公司列表
      companiesList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        belongingCompanyId: [
          {
            required: true,
            message: "项目所属公司id不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getlistBelongingCompany();
  },
  methods: {
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      console.log("111111111", this.queryParams);
      listContractProject(this.queryParams).then((response) => {
        this.ContractProjectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询公司列表
    getlistBelongingCompany() {
      listBelongingCompany().then((res) => {
        console.log(res);
        this.companiesList = res.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contractProjectId: null,
        name: null,
        belongingCompanyId: null,
        startTime: null,
        endTime: null,
        state: null,
        addUserId: null,
        addTime: null,
        updateUserId: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.startTime = null;
      this.queryParams.endTime = null;
      console.log(this.value1);
      if (this.value1) {
        this.queryParams.startTime = this.value1[0];
        this.queryParams.endTime = this.value1[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.value1 = null;
      this.queryParams.startTime = null;
      this.queryParams.endTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.contractProjectId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加项目";
      this.$router.push({
        path: "/contract/ContractProject/info",
        query: {
          title: "新增项目信息",
        },
      });
    },
    //查看按钮
    getRouter(row) {
      this.$router.push({
        path: "/contract/ContractProject/info",
        query: {
          title: "查看项目信息",
          contractProjectId: row.contractProjectId,
        },
      });
    },
    //修改按钮
    putRouter(row) {
      this.$router.push({
        path: "/contract/ContractProject/info",
        query: {
          title: "修改项目信息",
          contractProjectId: row.contractProjectId,
        },
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contractProjectId = row.contractProjectId || this.ids;
      getContractProject(contractProjectId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.contractProjectId != null) {
            updateContractProject(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContractProject(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contractProjectIds = row.contractProjectId || this.ids;
      this.$modal
        .confirm("此操作将永久删除，是否继续？")
        .then(function () {
          return delContractProject(contractProjectIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/ContractProject/export",
        {
          ...this.queryParams,
        },
        `ContractProject_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
